package net.homelinux.chaoswg.io.renga.krypto.idea;

import java.math.BigInteger;

import net.homelinux.chaoswg.io.higherorderfunctions.AbstractBinaryFunction;
import net.homelinux.chaoswg.io.higherorderfunctions.BinaryFunction;
import net.homelinux.chaoswg.io.renga.krypto.datatypes.numerical.Unsigned16Bit;
import net.homelinux.chaoswg.io.renga.krypto.datatypes.numerical.UnsignedNBit;

public class IDEA_Mult extends AbstractBinaryFunction<UnsignedNBit, UnsignedNBit, UnsignedNBit> implements BinaryFunction<UnsignedNBit, UnsignedNBit, UnsignedNBit> {

    public UnsignedNBit apply(UnsignedNBit a, UnsignedNBit b) {
        BigInteger zero = new BigInteger ("0");
        BigInteger exp2_16 = new BigInteger ("65536") ;
        BigInteger aVal = a.getValue () ;

        BigInteger bVal = b.getValue ();
        if (aVal.equals(zero)) {
            aVal = exp2_16;
        }
        if (bVal.equals(zero)) {
            bVal = exp2_16;
        }
        BigInteger cVal = aVal.multiply (bVal).mod (exp2_16.add(new BigInteger ("1")));

        if(cVal.equals (exp2_16)) {
            cVal = zero;
        }
        return new Unsigned16Bit (cVal);
    }
}
